import 'package:flutter_app/utils/toast_utils.dart';
import 'package:permission_handler/permission_handler.dart';

export 'package:permission_handler/permission_handler.dart';

/**
 * @Description: 权限管理工具类
 * @Author: 下雨了
 * @CreateDate: 2022-07-13 17:36
 * @UpdateUser: 更新者
 * @UpdateDate:  2022-07-13 17:36
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
///
//
// dependencies:
//   permission_handler: ^10.0.0
//

class PermissionUtils {
  // 私有构造函数
  PermissionUtils._internal();

  // 保存单例
  static final PermissionUtils _permissionUtils = PermissionUtils._internal();

  // 工厂构造函数
  factory PermissionUtils() => _permissionUtils;

  /// 文件读取权限，是否被授权，
  Future<bool> getStorage() async {
    return await Permission.storage.request().isGranted;
  }

  /// 手机状态
  Future<bool> getPhone() async {
    return await Permission.phone.request().isGranted;
  }

  /// 相机
  Future<bool> getCamera() async {
    PermissionStatus status = await Permission.camera.request();
    return nextPermissionStatus(status, "权限中开启相机权限");
  }

  /// 麦克风
  Future<bool> getMicrophone() async {
    PermissionStatus status = await Permission.microphone.request();
    return nextPermissionStatus(status, "权限中开启麦克风权限");
  }

  /// 定位权限
  Future<bool> getLocation() async {
    PermissionStatus status = await Permission.location.request();
    return nextPermissionStatus(status, "权限中开启GPS定位权限");
  }

  /// 通知权限
  Future<bool> getNotification() async {
    PermissionStatus status = await Permission.notification.request();
    return nextPermissionStatus(status, "权限中开启通知权限");
  }

  // 权限下一步操作
  bool nextPermissionStatus(PermissionStatus status, String content) {
    bool state = false;
    switch (status) {
      // 用户拒绝了
      case PermissionStatus.denied:
        break;
      // 用户同意
      case PermissionStatus.granted:
        state = true;
        break;
      // 被永久拒绝，不在显示请求对话框，但可以在设置中更改权限（仅 android）
      case PermissionStatus.permanentlyDenied:
        toast.show("在设置-应用-效能监督-$content");
        // BrnDialogManager.showConfirmDialog(
        //   Get.context!,
        //   cancel: "取消",
        //   confirm: "去设置",
        //   title: "",
        //   onConfirm: () async {
        //     // 跳转到App设置
        //     bool b = await openAppSettings();
        //     if (!b) {
        //       toast.show("无法打开相关设置，请手动在设置中开启相关权限");
        //     }
        //     Get.back();
        //   },
        //   onCancel: () {
        //     Get.back();
        //   },
        // );
        break;
      default:
    }
    return state;
  }

  /// 定位服务
  Future<bool> getLocationService() async {
    ServiceStatus status = await Permission.location.serviceStatus;
    return nextServiceStatus(status, "请开启位置服务");
  }

  // 服务下一步操作
  bool nextServiceStatus(ServiceStatus status, String content) {
    bool state = false;
    switch (status) {
      case ServiceStatus.enabled:
        state = true;
        break;
      case ServiceStatus.disabled:
        toast.show(content);
        break;
      case ServiceStatus.notApplicable:
        state = true;
        toast.show("当前平台上没有相关联的服务");
        break;
      default:
    }
    return state;
  }
}
